package ye.guo.huang.send.controller;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import ye.guo.huang.datatransferdemo.model.HeartBeatDTO;
import ye.guo.huang.datatransferdemo.model.RequestData;
import ye.guo.huang.datatransferdemo.model.ResponseResult;
import ye.guo.huang.datatransferdemo.utils.AESUtils;
import ye.guo.huang.datatransferdemo.utils.SignUtils;

@RestController
@Slf4j
public class HeartBeatController {
    private static final String APPID = "52226dac949c41ada5552ce3b2b69840";
    private static final String SECRET = "949c41ada5552ce3";

    /**
     * 发送心跳
     *  报文示例 {
     *     "appid":"52226dac949c41ada5552ce3b2b69840",
     *     "sign":"d611c6f57691fd78c28aa66c19c02601",
     *     "timestamp":111111,
     *     "data":"Oik1AAjN43nCoitAKC88cSEw6T1/Vjj1VOm3gH3qHxiAoKhbXmP/JNStqsJpnNpC0kVTd9ofIfMLKKv08/308aG8jgZiMO+czv10egTvMVk="
     *      }
     * @return
     */
    @PostMapping({"/heartBeat"})
    public ResponseResult heartBeat() throws Exception {
        HeartBeatDTO dto = new HeartBeatDTO();
        String json = JSON.toJSONString(dto);
        log.info("json={}",json);
        String encrypt = AESUtils.Encrypt(json, SECRET);
        RequestData requestData = new RequestData();
        requestData.setAppid(APPID);
        requestData.setData(encrypt);
        requestData.setTimestamp(System.currentTimeMillis());
        String sign = SignUtils.getMD5(requestData.sortData(SECRET), 32).toLowerCase();
        requestData.setSign(sign);
        log.info("要发送的密文requestData={}",requestData);
        String body = HttpRequest.post("127.0.0.1:806/heartBeat").body(JSON.toJSONString(requestData)).execute().body();
        return ResponseResult.ok(body);
    }

}
